package cn.icons;

import com.intellij.openapi.util.IconLoader;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;

/**
 * 插件图标
 *
 * @author butterfly
 * @date 2023-12-06
 */
public interface PluginIcons {

    Icon getButterfly = IconLoader.getIcon("/icons/butterfly.svg", PluginIcons.class);
    Icon BUTTERFLY = createScaledIcon(getButterfly, 10, 10);
    private static Icon createScaledIcon(Icon butterfly, int width, int height) {
        if (butterfly instanceof ImageIcon) {
            ImageIcon imageIcon = new ImageIcon(((ImageIcon) butterfly).getImage());
            // 使用 imageIcon
            Image image = imageIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
            return new ImageIcon(image);
        }else {
            ImageIcon imageIcon = convertIconToImageIcon(butterfly);
            Image image = imageIcon.getImage().getScaledInstance(width, height, Image.SCALE_SMOOTH);
            return new ImageIcon(image);
        }
    }
    static ImageIcon convertIconToImageIcon(Icon icon) {
        if (icon == null) {
            return null;
        }
        // 创建一个 BufferedImage
        int width = icon.getIconWidth();
        int height = icon.getIconHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
        // 将 Icon 绘制到 BufferedImage 中
        Graphics2D g2d = image.createGraphics();
        icon.paintIcon(null, g2d, 0, 0);
        g2d.dispose();
        // 使用 BufferedImage 创建 ImageIcon
        return new ImageIcon(image);
    }
}
